using System;
using System.Collections.Generic;
using UnityEngine;

namespace BTEditor
{
    [Serializable]
    public class BehaviourTree
    {
        public Dictionary<string, BTNode> BTNodeDic;

        public BTLineTree BTLineTree;
        
        public string TreeFileName;

        public bool IsAutoSave = true;
        
        /// <summary>
        /// 根节点
        /// </summary>
        public BTNode root;

        public BehaviourTree()
        {
            BTNodeDic = new Dictionary<string, BTNode>();  
            BTLineTree = new BTLineTree();
        }
        
        public void CreateRoot()
        {
            root = new BTNode("Root",new Root(),
                //取一个中间的位子
                new Rect((BTEditorConst.WINDOWS_WIDTH-BTEditorConst.LEFT_INSPECT_WIDTH)/2-BTEditorConst.Default_Width/2,50,BTEditorConst.Default_Width,BTEditorConst.Default_Height));
            AddNode(root);
        }
        
        public void Update(Rect canvas)
        {
            foreach (KeyValuePair<string,BTNode> keyValuePair in BTNodeDic)
            {
                keyValuePair.Value.Update(canvas);
            }
            BTLineTree.DrawLines();
        }

        public void AddNode(BTNode node)
        {
            BTNodeDic.Add(node.Guid,node);
        }

        public void RemoveNode(BTNode node)
        {
            BTNodeDic.Remove(node.Guid);
        }
    }
}